﻿using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace ZhongKeSite.Common.Configuration
{
    public static class ConfigurationService
    {
        private static IConfiguration configuration;
        public static IConfiguration GetConfiguration()
        {
            if (configuration == null)
            {
                var fileName = "appsettings.json";
                var directory = AppContext.BaseDirectory;
                directory = directory.Replace("\\", "/");
                var filePath = $"{directory}/{fileName}";
                if (!File.Exists(filePath))
                {
                    var length = directory.IndexOf("/bin");
                    filePath = $"{directory.Substring(0, length)}/{fileName}";
                }
                configuration = new ConfigurationBuilder().AddJsonFile(filePath, optional: true, reloadOnChange: true).Build();

                //configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                //    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                //    .Build();
            }
            return configuration;
        }
    }
}
